package com.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.dao.CarchargeMapper;
import com.yx.dao.OwnerMapper;
import com.yx.dao.ParkingMapper;
import com.yx.model.Carcharge;
import com.yx.model.Owner;
import com.yx.model.Parking;
import com.yx.service.ICarchargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class CarchargeServiceImpl extends ServiceImpl<CarchargeMapper, Carcharge> implements ICarchargeService {

    @Autowired
    private CarchargeMapper carchargeDao;

    @Autowired
    private OwnerMapper ownerMapper;

    @Autowired
    private ParkingMapper parkingMapper;


    @Override
    public PageInfo<Carcharge> findCarchargeAll(int page, int pagesize, Carcharge carcharge) {
        PageHelper.startPage(page, pagesize);
        List<Carcharge> list = carchargeDao.queryCarChargeAll(carcharge);
        PageInfo<Carcharge> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public IPage<Carcharge> findListByPage(Integer page, Integer pageCount) {
        IPage<Carcharge> wherePage = new Page<>(page, pageCount);
        Carcharge where = new Carcharge();

        return baseMapper.selectPage(wherePage, Wrappers.query(where));
    }

    @Override
    public int add(Carcharge carcharge) {
        return baseMapper.insert(carcharge);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(Carcharge carcharge) {
        return baseMapper.updateById(carcharge);
    }

    @Override
    public Carcharge findById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据条件分页查询
     *
     * @param carchargeIPage
     * @param carcharge
     * @return
     */
    @Override
    public Page<Carcharge> findCarchargeAll(Page<Carcharge> carchargeIPage, Carcharge carcharge) {
        Integer status = carcharge.getStatus();
        Parking parking = carcharge.getParking();

        // 查询条件
        QueryWrapper<Carcharge> wrapper = new QueryWrapper<>();
        String numbers="";
        // 判断是否为空
        if (!StringUtils.isEmpty(parking)) {

            if (!StringUtils.isEmpty(status)) {
                wrapper.eq("status", status);
            }

             numbers = parking.getNumbers();

        }

        Page<Carcharge> carchargePage = carchargeDao.selectPage(carchargeIPage, wrapper);


        // 进行数据封装
        List<Carcharge> records = carchargePage.getRecords();
        records = records.stream().map((carcharge1) -> {
            // 设置拥有者的信息
            Integer ownerId = carcharge1.getOwnerId();
            Owner owner = ownerMapper.selectById(ownerId);
            carcharge1.setOwner(owner);

            // 设置车位的信息
            Integer parkId = carcharge1.getParkId();
            Parking parking1 = parkingMapper.selectById(parkId);
            carcharge1.setParking(parking1);
            return carcharge1;
        }).collect(Collectors.toList());

        if (!StringUtils.isEmpty(numbers)) {
            String finalNumbers = numbers;
            records = records.stream().filter(carcharge1 -> {
                Parking parking1 = carcharge1.getParking();
                String numbers1 = parking1.getNumbers();

                if (numbers1.equals(finalNumbers)) {
                    return true;
                } else {
                    return false;
                }
            }).collect(Collectors.toList());
        }
        // 重新封装
        carchargePage.setRecords(records);
        return carchargePage;
    }
}
